package com.controller.agent;

import com.biz.BdTeamBiz;
import com.biz.BillBiz;
import com.biz.CustomerBiz;
import com.controller.backend.BaseController;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.BdTeam;
import com.entity.model.BdUserinfo;
import com.entity.model.Bill;
import com.entity.model.Customer;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.util.IOUtil;
import com.github.pagehelper.PageInfo;
import com.qiniu.common.Config;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.util.Base64;
import com.util.HttpRequest;
import com.util.PosterUtil;
import com.util.SystemConstant;
import com.util.webpage.PageData;
import com.wechat.mina.ComponentUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created by jc on 2019/11/23.
 */
@Controller
@RequestMapping("/agent/security/bdTeam")
public class AgentBdTeamController extends BaseController<BdTeam> {
    @Autowired
    private BdTeamBiz bdTeamBiz;

    @Autowired
    private BillBiz billBiz;

    @Autowired
    private CustomerBiz customerBiz;

    //代理商信息
    @RequestMapping(value = "index")
    public String index() throws Exception {
        return "/agent/security/bdTeam/index";
    }

    @RequestMapping(value = "bdStatistics")
    public String bdStatistics() throws Exception {
        return "/agent/security/bdStatistics/list";
    }


    @RequestMapping(value = "statisticsDetail")
    public String statisticsDetail() throws Exception {
        return "/agent/security/bdStatistics/form";
    }

    @RequestMapping(value = "detail")
    @ResponseBody
    public Result detail(HttpSession session) {
        BdUserinfo managerInfo = (BdUserinfo) session.getAttribute(SystemConstant.SESSION_AGENT_ADMIN);
        return ResultGenerator.genSuccessResult(bdTeamBiz.findById(managerInfo.getBdTeamId()));
    }

    @RequestMapping(value = "getMinaQr")
    @ResponseBody
    public Result getMinaQr(Integer bdId, Integer bdUserId) throws Exception {
        if (null == bdId) {
            return ResultGenerator.genFailResult("代理商Id不能为空");
        }

        String qrUrl = null;
        String page = null;
        if(null == bdUserId){
            bdUserId=0;
        }
        String str = bdId + "," + bdUserId;
        String scene = Base64.encode(str.getBytes("UTF-8"));
        try {
            page = "pages/my/vipAdd/index";
            qrUrl = ComponentUtil.getMinaQr("{\"scene\":\"" + scene + "\",\"page\":\"" + page + "\"}", page + scene);
        } catch (WeixinException e) {
            e.printStackTrace();
        }

        if (StringUtils.isNotBlank(qrUrl)) {
            return ResultGenerator.genSuccessResult(qrUrl);
        } else {
            return ResultGenerator.genFailResult("获取分享小程序二维码失败，请稍后重试");
        }
    }


    @RequestMapping(value = "bdStatisticsList")
    @ResponseBody
    public Result bdStatisticsList(HttpSession session,HttpServletRequest request) {
        BdUserinfo managerInfo = (BdUserinfo) session.getAttribute(SystemConstant.SESSION_AGENT_ADMIN);

        PageData cond = new PageData(request);
        cond.put("bdTeamId",managerInfo.getBdTeamId());
        List<Bill> list = billBiz.bdStatisticsList(cond);
        PageInfo<Bill> pageInfo = new PageInfo<Bill>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }

    @RequestMapping(value = "pdCustomerList")
    @ResponseBody
    public Result pdCustomerList(HttpServletRequest request) {
        PageData cond = new PageData(request);
        List<Customer> list = customerBiz.pdCustomerList(cond);
        PageInfo<Customer> pageInfo = new PageInfo<Customer>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }
}
